home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1996 February: Tool Chest / Apple Developer CD Series Tool Chest February 1996 (Apple Computer)(1996).iso / Tool Chest / Testing & Debugging / General tools / Report Error 2.0 / StringUtilities.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-08-17  |  6.7 KB  |  250 lines  |  [TEXT/KAHL]

  1. /*================================================================================
  2.     StringUtilities.c
  3.     
  4.     ©1991 Greg Anderson
  5.     greggor@apple.com
  6.     
  7.     FREE DISTRIBUTION
  8.     
  9.     The possessor of this source code may use any portion of
  10.     it for any purpose, and I place no restriction on its
  11.     redistribution.
  12.  
  13.     Convienient string utility routines.  See the macros defined
  14.     in stringUtilities.h, too.
  15. ================================================================================*/
  16. #include "StringUtilities.h"
  17. #include <string.h>
  18.  
  19. #ifndef __TYPES__
  20. #include <Types.h>
  21. #endif
  22.  
  23. /*----------------------------------------------------------------------
  24.     Copy a Pascal string into a C string
  25. ----------------------------------------------------------------------*/
  26. void PtoCcpy( char* cstr, Str255 pstr )
  27. {
  28.     unsigned short        i = (unsigned char)pstr[0];
  29.     
  30.     cstr[i] = 0;
  31.     while( i > 0 )
  32.     {
  33.         cstr[i - 1] = pstr[i];
  34.         --i;
  35.     }
  36. }
  37.  
  38. /*----------------------------------------------------------------------
  39.     Copy a C string into a Pascal string
  40. ----------------------------------------------------------------------*/
  41. void CtoPcpy(  Str255 pstr, char* cstr )
  42. {
  43.     int        i = 1;
  44.     
  45.     while( (*cstr) && (i < 255) )
  46.         pstr[i++] = *cstr++;
  47.     pstr[0] = i - 1;
  48. }
  49.  
  50. /*----------------------------------------------------------------------
  51.     Compare a Pascal string with a C string
  52. ----------------------------------------------------------------------*/
  53. short PtoCcmp( Str255 pstr, char* cstr )
  54. {
  55.     char        tmp[256];
  56.     
  57.     PtoCcpy( tmp, pstr );
  58.     return( strcmp( tmp, cstr ) );
  59. }
  60.  
  61. /*----------------------------------------------------------------------
  62.     Copy one pascal string into another
  63. ----------------------------------------------------------------------*/
  64. pascal void pstrcpy( Str255 dest, Str255 src )
  65. {
  66.     unsigned char*        dp;
  67.     unsigned char*        sp;
  68.     short                i;
  69.     short                max;
  70.     
  71.     sp = (unsigned char*)src;
  72.     dp = (unsigned char*)dest;
  73.     max = *sp;
  74.     
  75.     for( i = 0; i <= max ; ++i )
  76.         *dp++ = *sp++;
  77. }
  78.  
  79. /*----------------------------------------------------------------------
  80.     Copy one pascal string onto the end of another
  81. ----------------------------------------------------------------------*/
  82. pascal void pstrcat( Str255 dest, Str255 src )
  83. {
  84.     unsigned char*        dp;
  85.     unsigned char*        sp;
  86.     unsigned char*        cp;
  87.     short                i;
  88.     short                max;
  89.     
  90.     sp = (unsigned char*)src;
  91.     dp = (unsigned char*)dest;
  92.     max = *sp;
  93.     /*
  94.     // Adjust past length & chars already in dest
  95.     */
  96.     cp = dp + *dp + 1;
  97.     ++sp;
  98.     /*
  99.     // Determine how many characters we can really copy
  100.     */
  101.     if( max + *dp > 255 )
  102.         max = 255 - *dp;
  103.     *dp += max;
  104.     
  105.     for( i = 0; i < max ; ++i )
  106.         *cp++ = *sp++;
  107. }
  108.  
  109. /*----------------------------------------------------------------------
  110.     Compare two Pascal strings
  111. ----------------------------------------------------------------------*/
  112. short pstrcmp( unsigned char* a, unsigned char* b )
  113. {
  114.     short        len = ( *a < *b ? *a : *b );
  115.     short        lenEqual = ( *a > *b ) - ( *a < *b );
  116.     short        equalityTest;
  117.     
  118.     while( len )
  119.     {
  120.         ++a;
  121.         ++b;
  122.         equalityTest = ( *a > *b ) - ( *a < *b );
  123.         
  124.         if( equalityTest )
  125.             return equalityTest;
  126.         
  127.         --len;
  128.     }
  129.     
  130.     return lenEqual;
  131. }
  132.  
  133. /*-------------------------------------------------------------------
  134.     'Partial' string compare - check if s2 appears at the beginning
  135.     of s1.
  136.     
  137.     Identical to 'strncmp(s1,s2,strlen(s2));', with the bonus feature
  138.     of being case-insensitive.
  139. -------------------------------------------------------------------*/
  140. short partialstrcmp( register char* s1, register char* s2)
  141. {
  142.     register char    c1,
  143.                     c2;
  144.     
  145.     while(    c2 = *s2++ )
  146.     {
  147.         c1 = *s1++;
  148.         c1 = ToUpper(c1);
  149.         c2 = ToUpper(c2);
  150.         if( c1 < c2 ) return(-1);
  151.         if( c1 > c2 ) return( 1);
  152.     }
  153.     return(0);
  154. }
  155.  
  156. /*-------------------------------------------------------------------
  157.     Convert an ascii number into a string, advance the string ptr
  158.     past the number
  159. -------------------------------------------------------------------*/
  160. short ScanNumberInString( char** line )
  161. {
  162.     short n = 0;
  163.     while( (**line >= '0') && (**line <= '9') )
  164.     {
  165.         n = n * 10 + (**line - '0');
  166.         ++(*line);
  167.     }
  168.     return n;
  169. }
  170.  
  171. /*-------------------------------------------------------------------
  172.     Convert an ascii number into a string
  173. -------------------------------------------------------------------*/
  174. short NumberInString( char* line )
  175. {
  176.     return( ScanNumberInString( &line ) );
  177. }
  178.  
  179. /*-------------------------------------------------------------------
  180.     Copy characters from **line into the specified pstr
  181. -------------------------------------------------------------------*/
  182. void ScanTextInString( char** line, Str255 pstr, Boolean terminateAtSpace )
  183. {
  184.     short        len = 0;
  185.     
  186.     while( (len < 256) && ( (**line > ' ') || ( (**line == ' ') && (terminateAtSpace == false)) ) )
  187.     {
  188.         ++len;
  189.         pstr[len] = **line;
  190.         (*line)++;
  191.     }
  192.     pstr[0] = len;
  193. }
  194.  
  195. /*-------------------------------------------------------------------
  196.     Copy the next word from **line into the specified pstr
  197. -------------------------------------------------------------------*/
  198. void ScanWordInString( char** line, Str255 pstr )
  199. {
  200.     ScanTextInString( line, pstr, true );
  201. }
  202.  
  203. /*-------------------------------------------------------------------
  204.     Copy the next line from **line into the specified pstr
  205. -------------------------------------------------------------------*/
  206. void ScanLineInString( char** line, Str255 pstr )
  207. {
  208.     ScanTextInString( line, pstr, false );
  209. }
  210.  
  211. /*-------------------------------------------------------------------
  212.     Add a hex character to a string.  Always adds 1 character
  213. -------------------------------------------------------------------*/
  214. void AddHexCharToString( char* where, unsigned short hexNumber )
  215. {
  216.     hexNumber &= 0xF;
  217.     
  218.     if( hexNumber < 10 )
  219.         *where = '0' + hexNumber;
  220.     else
  221.         *where = 'A' + (hexNumber - 10);
  222. }
  223.  
  224. /*-------------------------------------------------------------------
  225.     Add a hex byte to a string.  Always adds 2 characters
  226. -------------------------------------------------------------------*/
  227. void AddHexByteToString( char* where, unsigned short hexNumber )
  228. {
  229.     AddHexCharToString( where, hexNumber >> 4 );
  230.     AddHexCharToString( where + 1, hexNumber & 0xF );
  231. }
  232.  
  233. /*-------------------------------------------------------------------
  234.     Add a hex shortword to a string.  Always adds 4 characters
  235. -------------------------------------------------------------------*/
  236. void AddHexShortToString( char* where, unsigned short hexNumber )
  237. {
  238.     AddHexByteToString( where, hexNumber >> 8 );
  239.     AddHexByteToString( where + 2, hexNumber & 0xFF );
  240. }
  241.  
  242. /*-------------------------------------------------------------------
  243.     Add a hex longword to a string.  Always adds 8 characters
  244. -------------------------------------------------------------------*/
  245. void AddHexLongToString( char* where, unsigned long hexNumber )
  246. {
  247.     AddHexShortToString( where, hexNumber >> 16 );
  248.     AddHexShortToString( where + 4, hexNumber & 0xFFFF );
  249. }
  250.